<?php
/**
 * Created by PhpStorm.
 * User: cen
 * Date: 2020/6/29
 * Time: 22:28
 */

namespace app\admin\controller;


use think\Controller;
use app\admin\model\NewsModel;
use think\facade\App;

class News extends Controller
{
    public function index(){
        return $this->fetch();
    }

    public function tableData(){

        $list = NewsModel::paginate(10)->toArray();

        return json([
            'code' => 0,
            'msg' => '',
            'count' => $list['total'],
            'data' => $list['data'],
        ]);
    }

    public function form(){
        return $this->fetch();
    }

    public function submitForm(){

        $data = $this->request->param();

        if(!isset($data['title'])){
            return json('确实参数',400);
        }

        if(!isset($data['type'])){
            return json('确实参数',400);
        }

        if(!isset($data['content'])){
            return json('确实参数',400);
        }


       // try{
            NewsModel::create([
                'title' => $data['title'],
                'content' => $data['content'],
                'type' =>$data['type'],
            ]);
        //}catch (\Exception $e){
         //   return json('系统异常',400);
        //}


        return json('ok');

    }

    public function upload(){
        $imgage = $this->request->file('file');

        //$f = ;
        $path = App::getRootPath().'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'images';
        $info = $imgage->move( $path);

        $src= '/uploads/images/'.$info->getSaveName();



        return json([
            "errno"=>0,

             // data 是一个数组，返回若干图片的线上地址
            "data"=> [
                $src
                ]
        ]);
    }

    public function del(){
        $id = $this->request->param('id');

        $res = NewsModel::find($id);

        $res->status = 1;
        $res->save();
    }
}